import { _decorator, Component, Node, Mesh, CCBoolean, LODGroup, instantiate, MeshRenderer, Vec3, LOD } from 'cc';
import { EDITOR } from 'cc/env';
const { ccclass, property, executeInEditMode } = _decorator;
/**
 * lod-group 管理
 * 主要用于lod 优化，
 * 优化点：减少编辑时场景中节点的数量，优化预制体大小以及启动时候的加载时间
 * 注意：当前脚本所在节点未同一类型lod-group所在父节点，运行时会将第一个节点的lod-group的所有属性以及子节点同步到其他所有的子节点
 */
@ccclass('LodGroupController')
@executeInEditMode
export class LodGroupController extends Component {

    @property
    _editorReset: boolean = null!;
    @property({ type: CCBoolean })
    public set editorReset(val) {
        if (val === true) {
            this.editorLodGroupRefresh();
        }
        this._editorReset = false;
    }
    public get editorReset() {
        return this._editorReset;
    }

    onLoad() {

    }
    private editorLodGroupRefresh(): void {
        let child: Node[] = this.node.children;
        let first: Node = child[0];
        let firstChild: Node[] = first.children;
        let meshRenderer: MeshRenderer = firstChild[0].getComponent(MeshRenderer)!;
        for (let i = 1; i < child.length; i++) {
            let target: Node = child[i];
            target.destroyAllChildren();
            //删除原有的lod-group
            let targetLodGropu: LODGroup = target.getComponent(LODGroup)!;
            if (targetLodGropu) {
                targetLodGropu.destroy();
            }
            let node: Node = new Node();
            let mRender: MeshRenderer = node.addComponent(MeshRenderer);
            mRender.mesh = meshRenderer.mesh;
            mRender.sharedMaterials = meshRenderer.sharedMaterials;
            target.addChild(node);
        }

        // let lodGroup: LODGroup = first.getComponent(LODGroup)!;
        // if (lodGroup) {
        //     lodGroup.destroy();

        // } else {
        //     console.log("请为第一个节点绑定lod-group 组件");
        // }
    }
    /**
     * 刷新lod-group
     */
    public lodGroupRefresh(): void {

        console.log("lod group:", this.node.name);
        // return;
        let child: Node[] = this.node.children;
        let first: Node = child[0];

        let lodGroup: LODGroup = first.getComponent(LODGroup)!;
        if (lodGroup) {
            for (let i = 1; i < child.length; i++) {
                this.allpy(first, child[i]);
            }
        } else {
            console.log("请为第一个节点绑定lod-group 组件");
        }
    }
    private allpy(base: Node, target: Node): void {
        //删除目标节点上原有的子节点
        target.destroyAllChildren();
        //base节点绑定 lod-group
        let baseLodGroup: LODGroup = base.getComponent(LODGroup)!;
        let targetLodGropu: LODGroup = target.getComponent(LODGroup)!;
        if (!targetLodGropu) {
            targetLodGropu = target.addComponent(LODGroup)!;
        }

        //同步lod-group数据
        targetLodGropu.objectSize = baseLodGroup.objectSize;
        let lods: LOD[] = [];
        //拷贝base 的所有子节点到target 节点下
        let baseChild: Node[] = base.children;
        for (let i = 0; i < baseChild.length; i++) {
            let baseNode: Node = baseChild[i];
            let baseMeshRender: MeshRenderer = baseNode.getComponent(MeshRenderer)!;
            if (baseMeshRender) {
                let node: Node = new Node();
                let targetMeshRender: MeshRenderer = node.addComponent(MeshRenderer);
                targetMeshRender.shadowNormalBias = baseMeshRender.shadowNormalBias;
                targetMeshRender.shadowBias = baseMeshRender.shadowBias;
                targetMeshRender.shadowCastingMode = baseMeshRender.shadowCastingMode;
                targetMeshRender.receiveShadow = baseMeshRender.receiveShadow;
                targetMeshRender.mesh = baseMeshRender.mesh;
                targetMeshRender.sharedMaterials = baseMeshRender.sharedMaterials;
                target.addChild(node);


                let lod: LOD = new LOD();
                lod.screenUsagePercentage = baseLodGroup.LODs[i].screenUsagePercentage;
                lod.insertRenderer(0, targetMeshRender);
                lods.push(lod);
            }
        }
        targetLodGropu.LODs = lods;
    }
}


